﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment_5_Amanda_Boström.ContactFiles
{
    public class ContactManager
    {
        private List<Contact> m_contactRegistry;

        public ContactManager()
        {
            m_contactRegistry = new List<Contact>();
        }
        /// <summary>
        /// Returns the number of contacts in the registry.
        /// </summary>
        /// <returns></returns>
        public int Count()
        {
            int contacts;
            contacts = m_contactRegistry.Count();
            return contacts;
        }
        /// <summary>
        /// Adds a new contact to the registry.
        /// </summary>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="addressIn"></param>
        /// <returns></returns>
        public bool AddContact(string firstName, string lastName, Address addressIn)
        {
            Contact contact = new Contact(firstName, lastName, addressIn);
            if (contact != null)
            {
                m_contactRegistry.Add(contact);
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// Also adds a contact to the registry, taking only one parameter.
        /// </summary>
        /// <param name="contactIn"></param>
        /// <returns></returns>
        public bool AddContact(Contact contactIn)
        {
            if (contactIn != null)
            {
                m_contactRegistry.Add(contactIn);
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// Replaces the contact information at the specified index.
        /// </summary>
        /// <param name="contactIn"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public bool ChangeContact(Contact contactIn, int index)
        {
            if (contactIn != null)
            {
                m_contactRegistry.Insert(index, contactIn);
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// Removes a contact at the specified index.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public bool DeleteContact(int index)
        {
            m_contactRegistry.RemoveAt(index);
            return true;
        }
        /// <summary>
        /// Returns the contact at the specified index.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Contact GetContact(int index)
        {
            if (index < 0 || index >= m_contactRegistry.Count)
            {
                return null;
            }
            else
            {
                return m_contactRegistry[index];
            }
        }
        /// <summary>
        /// Returns an array of strings where every string represents a contact.
        /// </summary>
        /// <returns></returns>
        public string[] GetContactsInfo()
        {
            string[] strInfoStrings = new string[m_contactRegistry.Count];

            int i = 0;
            foreach (Contact contactObj in m_contactRegistry)
            {
                strInfoStrings[i++] = contactObj.ToString();
            }
            return strInfoStrings;
        }
    }
}
